// tradeJob.test.ts
interface TradeJob {
  id?: number;
  name: string;
  level: "FIRST" | "SECOND" | "THIRD" | "FOURTH" | "FIFTH";
  mode: "SINGLE" | "COMPREHENSIVE";
  brief?: string;
  categoryList?: string[];
  certifyTips?: string[];
  expiredTime?: string; // 格式: "HH:mm:ss"
}

const API_BASE = "http://127.0.0.1:1080/api";
const CREATED_IDS: number[] = [];

interface Result<T> {
  code: number;
  message: string;
  data: T;
}

async function createTradeJob(job: TradeJob): Promise<TradeJob> {
  const response = await fetch(`${API_BASE}/trade-job/create`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(job),
  });

  if (!response.ok) {
    throw new Error(`创建失败: ${response.status} ${await response.text()}`);
  }

  const result = await response.json();
  return result.data;
}

async function updateTradeJob(job: TradeJob): Promise<TradeJob> {
  const response = await fetch(`${API_BASE}/trade-job/update`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(job),
  });

  if (!response.ok) {
    throw new Error(`更新失败: ${response.status} ${await response.text()}`);
  }

  const result = await response.json();
  return result;
}

async function deleteTradeJob(id: number): Promise<boolean> {
  const response = await fetch(`${API_BASE}/trade-job/delete/${id}`, {
    method: "POST",
  });

  if (!response.ok) {
    throw new Error(`删除失败: ${response.status} ${await response.text()}`);
  }

  const result = await response.json();
  return result;
}

async function getTradeJobPage(page = 1, size = 10): Promise<any> {
  const response = await fetch(`${API_BASE}/trade-job/page`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ page, size }),
  });

  if (!response.ok) {
    throw new Error(
      `分页查询失败: ${response.status} ${await response.text()}`
    );
  }

  const res = await response.json();
  return res.data;
}

// 测试用例
async function runTests() {
  console.log("开始测试 /trade-job API...");

  try {
    // 1. 创建100条数据
    console.log("1. 创建100条数据...");
    for (let i = 1; i <= 100; i++) {
      const job: TradeJob = {
        name: `任务${i}`,
        level: "FIRST",
        mode: "SINGLE",
        brief: `这是任务${i}的简介`,
        categoryList: [`分类${i % 5}`],
        certifyTips: [`提示${i % 3}`],
        expiredTime: "23:59:59",
      };

      if (i > 50) {
        job.level = "SECOND";
        job.mode = "COMPREHENSIVE";
      }

      const result = await createTradeJob(job);
      CREATED_IDS.push(result.id);
      console.log(`创建任务${i}成功，ID: ${result.id}`);
    }

    // 2. 修改前5条数据
    console.log("2. 修改前5条数据...");
    for (let i = 0; i < 5; i++) {
      const id = CREATED_IDS[i];
      const job: TradeJob = {
        id,
        name: `修改后的任务${i + 1}`,
        level: "THIRD",
        mode: "COMPREHENSIVE",
        brief: `这是修改后的任务${i + 1}的简介`,
        categoryList: ["修改后的分类"],
        certifyTips: ["修改后的提示"],
        expiredTime: "12:00:00",
      };

      const result = await updateTradeJob(job);
      console.log(`修改任务${id}成功`);
    }

    // 3. 删除最后5条数据
    console.log("3. 删除最后5条数据...");
    for (let i = 95; i < 100; i++) {
      const id = CREATED_IDS[i];
      const result = await deleteTradeJob(id);
      console.log(`删除任务${id}成功: ${result}`);
    }

    // 4. 分页查询验证
    console.log("4. 分页查询验证...");
    const pageResult = await getTradeJobPage(1, 20);
    console.log(`查询到 ${pageResult.records.length} 条记录`);

    console.log("所有测试完成！");
  } catch (error) {
    console.error("测试过程中出现错误:", error);
  }
}

// 运行测试
runTests();
